package jgit;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {

        File directory = new File("");

        // clone远程仓库
        Git git = Git.cloneRepository()
                .setURI( "https://gitee.com/jinbin/tmUtils.git" )
                .setCredentialsProvider(new UsernamePasswordCredentialsProvider( "bin.jinb@qq.com", "jb198710" ))
                .setDirectory(new File(directory.getAbsolutePath() + "./repo"))
                .call();

        //
        git.diff().setOutputStream( System.out ).call();

        git.close();

        // 读取已有仓库
        Repository repo = new FileRepositoryBuilder()
                //.setGitDir(new File("c:/temp/jgit-test/.git"))
                .setGitDir(Paths.get("./repo/", ".git").toFile())
                .build();

        // master
        repo.getBranch();
        // refs/heads/master
        repo.getFullBranch();
        // Ref[refs/heads/master=c2f17ed95d2d7223827cab28e8fcbf7386460cbf(-1)]
        Ref head = repo.findRef("refs/heads/" + repo.getBranch());

        // 输出所有commit信息
        List<String> commits = new ArrayList<>();
        try (RevWalk revWalk = new RevWalk(repo)) {
            revWalk.markStart(revWalk.parseCommit(head.getObjectId()));
            for (RevCommit revCommit : revWalk) {
                commits.add(revCommit.getFullMessage());
                System.out.println("\nCommit-Message: " + revCommit.getFullMessage());
            }
            revWalk.dispose();
        }

        // 比较不同commit之间的改动
        // commit c2f17ed95d2d7223827cab28e8fcbf7386460cbf 1638192436 -----sp
        // 最近一次提交
        RevCommit headCommit = getHeadCommit(repo);
        // commit b1abdcfb3ca7e9b71b34799fba22631273b1dff5 1638184438 -----sp
        // 倒数第二次提交
        RevCommit diffWith = headCommit.getParent(0);

        FileOutputStream stdout = new FileOutputStream(FileDescriptor.out);
        try (DiffFormatter diffFormatter = new DiffFormatter(stdout)) {
            diffFormatter.setRepository(repo);
            for (DiffEntry entry : diffFormatter.scan(diffWith, headCommit)) {
                diffFormatter.format(diffFormatter.toFileHeader(entry));
            }
        }
    }

    private static RevCommit getHeadCommit(Repository repo) throws Exception {
        try (Git git = new Git(repo)) {
            Iterable<RevCommit> history = git.log().setMaxCount(1).call();
            return history.iterator().next();
        }
    }
}
